﻿namespace VariableUsageAndNamingBestPractices
{
    using System;
    using System.Threading;
    using System.Globalization;

    public class Statistics
    {
        public static void Main(string[] args)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            double[] testArray = new double[] { 1, 2, 3, 4, -7, 5.5, 68.45 };
            PrintStatistics(testArray);
        }

        /// <summary>
        /// Print statistic for the numbers array including: mininum, maximum and average numbers.
        /// </summary>
        /// <param name="array">Array of double numbers</param>
        public static void PrintStatistics(double[] array)
        {
            var arrayLength = array.Length;
            PrintStatistics(array, arrayLength);
        }

        /// <summary>
        /// Print statistic for the numbers array including: mininum, maximum and average numbers.
        /// </summary>
        /// <param name="array">Array of double numbers</param>
        /// <param name="arrayLength">Numbers of the element from the begining which will be included in the statistics</param>
        public static void PrintStatistics(double[] array, int arrayLength)
        {
            double maxArrayNumber = GetMaxValue(array, arrayLength);
            Print(maxArrayNumber, "Max array value");

            double minArrayNumber = GetMinValue(array, arrayLength);
            Print(minArrayNumber, "Min array value");

            double averageArrayNumber = GetAverageValue(array, arrayLength);
            Print(averageArrayNumber, "Average array value");
        }

        private static void Print(double number, string titleText = "")
        {
            string textToPrint;
            if (titleText != "")
            {
                textToPrint = String.Format("{0}: {1,5:F2}", titleText, number);
            }
            else
            {
                textToPrint = number.ToString();
            }

            Console.WriteLine(textToPrint);
        }

        private static double GetMinValue(double[] array, int arrayLength)
        {
            double minValue = double.MaxValue;
            for (int i = 0; i < arrayLength; i++)
            {
                if (array[i] < minValue)
                {
                    minValue = array[i];
                }
            }

            return minValue;
        }

        private static double GetMaxValue(double[] array, int arrayLength)
        {
            double maxValue = double.MinValue;
            for (int i = 0; i < arrayLength; i++)
            {
                if (array[i] > maxValue)
                {
                    maxValue = array[i];
                }
            }

            return maxValue;
        }

        private static double GetAverageValue(double[] array, int arrayLength)
        {
            double sum = 0;
            for (int i = 0; i < arrayLength; i++)
            {
                sum += array[i];
            }

            return sum / arrayLength;
        }
    }
}